package com.cqhfan.common.autoconfigure.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author 眭传洪
 * @create 2023/12/26
 */
@Slf4j
@Configuration
@ConditionalOnClass({RedissonClient.class, Redisson.class})
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfig {

    private static final String REDIS_PROTOCOL_PREFIX = "redis://%s:%d";

    @Bean
    public RedissonClient redissonClient(RedisProperties redisProperties) {
        log.info("redisson初始化。。。");
        Config config = new Config();
        config.useSingleServer()
                .setAddress(String.format(REDIS_PROTOCOL_PREFIX, redisProperties.getHost(), redisProperties.getPort()))
                .setDatabase(0)
                .setTimeout(3000)
                .setPassword(redisProperties.getPassword());
        RedissonClient redissonClient = Redisson.create(config);
        log.info("redisson初始化完成。。。");
        return redissonClient;
    }

}
